package woodnaonly.radiobutto_test;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;

import woodnaonly.radiobutto_test.Fragment.ExFilePickerFagment;
import woodnaonly.radiobutto_test.Fragment.MultiImageSelectorFagment;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener
{

    RadioGroup mRadioGrop;
    PagerAdapter adapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRadioGrop = (RadioGroup) findViewById(R.id.radiogroup);
        adapter = new FragmentPagerAdapter(getSupportFragmentManager())
        {

            @Override
            public int getCount()
            {
               return mRadioGrop.getChildCount();
//                return 3;
            }

            @Override
            public Fragment getItem(int position)
            {
                Fragment fragment=null;
                switch (position)
                {
                    case 0:fragment = new MultiImageSelectorFagment();break;
                    case 1:fragment=new MultiImageSelectorFagment();break;
                    case 2:fragment=new ExFilePickerFagment();break;
                }
                return fragment;
            }
        };
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                Log.d("asdsad", position+"");
                switch (position)
                {
                    case 0:mRadioGrop.check(R.id.rabt01); break;
                    case 1:mRadioGrop.check(R.id.rabt02); break;
                    case 2:mRadioGrop.check(R.id.rabt03); break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });
        mRadioGrop.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.rabt01:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.rabt02:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.rabt03:
                mViewPager.setCurrentItem(2);
                break;

        }
    }


}
